package com.zx.map.service.impl;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;

import com.zx.map.utils.CommonResponseUtils;
import com.zx.map.utils.SUConstants;
import com.zx.platform.utils.TmDateUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import com.zx.map.dao.AgentInfoDao;
import com.zx.map.dao.AgentLeftDao;
import com.zx.map.dao.ProductDao;
import com.zx.map.dao.model.AgentInfo;
import com.zx.map.dao.model.AgentInfoExample;
import com.zx.map.dao.model.AgentLeft;
import com.zx.map.dao.model.AgentLeftExample;
import com.zx.map.dao.model.Product;
import com.zx.map.service.AgentLeftService;
import com.zx.map.service.ProductService;
import com.zx.map.service.domain.AgentLeftDomain;
import com.zx.map.service.model.AgentInfoModel;
import com.zx.map.service.model.AgentLeftModel;
import com.zx.map.service.model.ProductLeftModel;
import com.zx.map.service.model.ProductModel;
import org.springframework.transaction.annotation.Transactional;

/**
 * 
 * @author ysm
 * 
 */
@Service
public class AgentLeftServiceImpl implements AgentLeftService {

	@Autowired
	private AgentLeftDao agentLeftDao;
	@Autowired
	private AgentInfoDao agentInfoDao;
	@Autowired
	private ProductDao productDao;
	@Autowired
	private ProductService productService;


	
	/**
	 * 获取代理商分润比例
	 * 注意，最后一个是100%
	 */
	@Override
	public BigDecimal[] getleftRate(List<AgentInfo> agentInfoList, long productId)  {
		
		//查询商品总利润
		Product product = productDao.selectByPrimaryKey(productId);
		double totalProfit = 0d;
		if(product!=null){
			totalProfit=product.getTotalProfit();
		}

		// 得到各级代理商留扣
		int size = agentInfoList.size();
		int[] arr = new int[size];
		for(int i=0; i<size-1;i++){
			AgentInfo agentInfo=agentInfoList.get(i);
			AgentInfo agentInfoNext=agentInfoList.get(i+1);
			AgentLeftExample agentLeftExample=new AgentLeftExample();
			AgentLeftExample.Criteria creCriteria=  agentLeftExample.createCriteria();
			creCriteria.andAgentIdEqualTo(agentInfo.getAgentId());
			creCriteria.andProductIdEqualTo(productId);
			creCriteria.andRoleIdEqualTo(Long.valueOf(agentInfoNext.getAgentLevel()));
			List<AgentLeft>agentLeftList = agentLeftDao.getListByExample(agentLeftExample);
			if(agentLeftList.isEmpty()){
				arr[i]=0;
			}else{
				arr[i] = agentLeftList.get(0).getLeftRate();
			}
		}
		arr[size-1]=100;
		//leftRate保存上一次剩余
		BigDecimal leftRate = BigDecimal.ONE;
		BigDecimal[] agentRate = new BigDecimal[size];
		for(int i=0; i<size; i++){
			agentRate[i] = new BigDecimal(totalProfit/10000f).multiply(leftRate).multiply(new BigDecimal(arr[i]/100f)).setScale(6,BigDecimal.ROUND_UP);
			leftRate = leftRate.multiply(new BigDecimal((100-arr[i])/100f)).setScale(6,BigDecimal.ROUND_UP);;
		}

		return agentRate;
	}

	/**
	 * 产品留扣列表查询
	 */
	@Override
	public List<ProductLeftModel> getModelByAgentId(
			AgentInfoModel agentInfoModel, long roleId) {
		List<ProductLeftModel> productLeftModels = new ArrayList<ProductLeftModel>();

		List<ProductModel> productList = productService.getAllList();
		int productSize = productList.size();
		String bsid = agentInfoModel.getBsid();
		String[] agentIdArray = bsid.split("-");
		List<String> agentIdList = Arrays.asList(agentIdArray).subList(1,agentIdArray.length);
		AgentInfoExample agentInfoExample = new AgentInfoExample();
		AgentInfoExample.Criteria creCriteria = agentInfoExample.createCriteria();
		creCriteria.andAgentIdIn(agentIdList);
		List<AgentInfo> agentInfoList = agentInfoDao.getListByExample(agentInfoExample);
		
		for (int i = 0; i < productSize; i++) {
			ProductModel productModel = productList.get(i);
			ProductLeftModel productLeftModel = new ProductLeftModel();
			BeanUtils.copyProperties(productModel, productLeftModel);

			AgentLeftExample agentLeftExample = new AgentLeftExample();
			AgentLeftExample.Criteria agentLeftCriteria = agentLeftExample
					.createCriteria();
			agentLeftCriteria.andAgentIdEqualTo(agentInfoModel.getAgentId());
			agentLeftCriteria.andProductIdEqualTo(productModel.getProductId());
			agentLeftCriteria.andRoleIdEqualTo(roleId);
			List<AgentLeft> agentLeftList = agentLeftDao.getListByExample(agentLeftExample);
			if (agentLeftList.isEmpty()) {
				productLeftModel.setLeftRate(0);
			} else {
				productLeftModel.setLeftRate(agentLeftList.get(0).getLeftRate());
				productLeftModel.setCussId(agentLeftList.get(0).getCussId());
			}

			BigDecimal agentTotleProfit = productModel.getTotalProfit();
			for (int j = 0; j < agentInfoList.size() - 1; j++) {
				AgentInfo agentInfo = agentInfoList.get(j);
				AgentInfo agentInfoNext = agentInfoList.get(j + 1);
				agentLeftExample = new AgentLeftExample();
				agentLeftCriteria = agentLeftExample.createCriteria();
				agentLeftCriteria.andAgentIdEqualTo(agentInfo.getAgentId());
				agentLeftCriteria.andProductIdEqualTo(productModel.getProductId());
				agentLeftCriteria.andRoleIdEqualTo(Long.valueOf(agentInfoNext.getAgentLevel()));
				int agentLeftRate=0;
				agentLeftList = agentLeftDao.getListByExample(agentLeftExample);
				if(agentLeftList.isEmpty()){
					agentLeftRate=0;
				}else{
					 agentLeftRate = agentLeftList.get(0).getLeftRate();	
				}
				
				agentTotleProfit = agentTotleProfit.subtract(
						agentTotleProfit.multiply(new BigDecimal(
								agentLeftRate / 100f))).setScale(2,
						BigDecimal.ROUND_UP);
			}
			productLeftModel.setTotalProfit(agentTotleProfit);
			productLeftModels.add(productLeftModel);
		}
		return productLeftModels;
	}

	@Override
	@Transactional
	public HashMap<String, Object> saveOrUpdate(AgentInfoModel curAgentInfoModel,long roleId, ArrayList<AgentLeftModel> agentLeftModelArrayList) {
		   //删除代理商历史数据
		String agentId=curAgentInfoModel.getAgentId();
		  AgentLeftExample agentLeftExample=new AgentLeftExample();
		AgentLeftExample.Criteria criteria=agentLeftExample.createCriteria();
		 criteria.andAgentIdEqualTo(agentId);
		 criteria.andRoleIdEqualTo(roleId);
		agentLeftDao.deleteByByExample(agentLeftExample);
		 List<AgentLeft> agentInfoList=new ArrayList();
		for (AgentLeftModel agentLeftModel : agentLeftModelArrayList) {
			AgentLeft agentLeft=new AgentLeft();
			agentLeft.setAgentId(agentId);
			agentLeft.setRoleId(roleId);
			agentLeft.setProductId(agentLeftModel.getProductId());
			agentLeft.setLeftRate(agentLeftModel.getLeftRate());
			agentLeft.setCreateTime(TmDateUtil.nowTime());
			agentLeft.setUpdateTime(TmDateUtil.nowTime());
			agentInfoList.add(agentLeft);
		}
		int num=agentLeftDao.saveBatch(agentInfoList);
		if(num>0){
			return CommonResponseUtils.successResponse(SUConstants.MSG_SUCCESS_VALUE_SAVE);
		} else {
			return CommonResponseUtils.successResponse(SUConstants.MSG_ERROR_VALUE_SAVE);
		}
	}

}
